package com.dq.repository.statistics;

import com.dq.domain.org.Org;
import com.dq.domain.statistics.Statistics;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author liuhui
 * @date 2019/11/29 0029 16:24:50
 */
@Repository
public interface StatisticsRepository extends JpaRepository<Statistics,Long> {
    /**
     * 根据类型分页查询统计记录
     * @param type
     * @param pageable
     * @return
     */
    Page<Statistics> findByStatisticsTypeAndOrgOrderByCreateTimeAsc(Statistics.StatisticsType type, Org org, Pageable pageable);

    @Query(nativeQuery = true,value = "SELECT COALESCE(sum(sale_price),0) as price,FROM_UNIXTIME(count_time,'%y-%d') month FROM `statistics` WHERE statistics_type = ?1 GROUP BY FROM_UNIXTIME(count_time,'%Y-%m')")
    List<Map> countSaleByMonth(Statistics.StatisticsType statisticsType);
}
